$(function(){
    $("#username").blur(function(){
        let _that = this ; 
        ajax({
            url:'./php/register/reg.php',
            dataType:'json',
            contentType:true,
            method:'post',
            data:{
                username:_that.value
            }
        }).then(res=>{
            if( res.status == 1 ){
                _that.nextElementSibling.classList.add("active1");
            } else {
                _that.nextElementSibling.classList.remove("active1");
            }
        });
    });
    $("#tel").blur(function(){
        let _that = this ;
        let reg = /^1[3-9]\d{9}$/g;
        if( reg.test(this.value) ){
            _that.nextElementSibling.classList.remove("active1");
        } else {
            _that.nextElementSibling.classList.add("active1");
        }
    });
    $("#password2").blur(function(){
        let password1 = $("#password1")[0].value;
        let password2 = $("#password2")[0].value;
        if( password1 == password2 ){
            this.nextElementSibling.classList.remove("active1");
        } else {
            this.nextElementSibling.classList.add("active1");
        }
    });
    $(".logBtn").click(function(){
        let username = $("#username")[0].value;
        let tel = $("#tel")[0].value;
        let password1 = $("#password1")[0].value;
        let password2 = $("#password2")[0].value;
        if( password1 == password2 ){
            $("#password2")[0].nextElementSibling.classList.remove("active1");
            ajax({
                url:'./php/register/addUser.php',
                method:'post',
                dataType:'json',
                contentType:true,
                data:{
                    username,
                    tel,
                    password:password1,
                    ulevel:3
                }
            }).then(res=>{
                if( res.status == 1 ){
                    alert("注册成功");
                    window.location.href = './login.html';
                } else {
                    alert("注册失败，请刷新网页重新注册！");
                }
            });
        } else {
            $("#password2")[0].nextElementSibling.classList.add("active1");
        }
    });
});